﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class NetCounter_Tran : System.Web.UI.Page
{
    //private string c_debug = "0";
    //private string ExpTime = "12";
    private inc ii = new inc();
    private int i = 0;
    private void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                if (Request.ServerVariables["Remote_Addr"] != null && i++ == 0)
                {
                    if (Request.ServerVariables["Remote_Addr"].ToString().IndexOf(".") <= 0)
                    {
                        //0或者没有不显示,1显示图标,2显示文字
                        string style = "0";
                        if (Request["style"] != null)
                            style = Request["style"].ToString();
                        if (style == "1")
                            Style1();
                        else if (style == "2")
                            Style2();
                        else if (style == "3")
                            Style3();

                        return;
                    }
                    else
                    {
                        Loading();
                    }
                }
                //c_debug = "1";// System.Configuration.ConfigurationSettings.AppSettings["c_debug"];
                //ExpTime = "12";// System.Configuration.ConfigurationSettings.AppSettings["ExpTime"];   
            }
            catch (Exception ex)
            {
                Response.Write("document.write(\"" + ex.Message + "\");");
                return;
            }
        }
    }

    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
        //
        InitializeComponent();
        base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion

    private void Loading()
    {
        //收集需要统计的信息									
        string vIp = "";//访问者IP
        string vSoft = "";//访问者系统信息
        string vReferer = "";//来源
        string iReferer = "";
        if (Request.ServerVariables["Remote_Addr"] != null)
            vIp = Request.ServerVariables["Remote_Addr"];
        if (Request.ServerVariables["HTTP_USER_AGENT"] != null)
            vSoft = Request.ServerVariables["HTTP_USER_AGENT"];

        if (vIp.IndexOf(".") <= 0)
            vIp = "";

        if (Request["referer"] != null)
        {
            vReferer = Request["referer"];
            iReferer = Request["referer"];
        }

        if (vReferer == "")
        {
            vReferer = "直接输入网址进入的";
            iReferer = "直接输入网址进入的";
        }
        else
        {
            vReferer = vReferer.Substring(7);
            vReferer = "http://" + vReferer.Substring(0, vReferer.IndexOf("/"));
        }
        string vExplorer = "Other";
        if (vSoft.IndexOf("NetCaptor") > 0)
            vExplorer = "NetCaptor";
        else if (vSoft.IndexOf("MSIE 6") > 0)
            vExplorer = "Internet Explorer 6.x";
        else if (vSoft.IndexOf("MSIE 5") > 0)
            vExplorer = "Internet Explorer 5.x";
        else if (vSoft.IndexOf("MSIE 4") > 0)
            vExplorer = "Internet Explorer 4.x";
        else if (vSoft.IndexOf("Netscape") > 0)
            vExplorer = "Netscape";
        else if (vSoft.IndexOf("Opera") > 0)
            vExplorer = "Opera";

        string vOS = "Other";
        if (vSoft.IndexOf("Windows NT 5.0") > 0)
            vOS = "Windows 2000";
        else if (vSoft.IndexOf("Windows NT 5.1") > 0)
            vOS = "Windows XP";
        else if (vSoft.IndexOf("Windows NT 5.2") > 0)
            vOS = "Windows 2003";
        else if (vSoft.IndexOf("Windows NT") > 0)
            vOS = "Windows NT";
        else if (vSoft.IndexOf("Windows 9") > 0)
            vOS = "Windows 9x";
        else if (vSoft.IndexOf("unix") > 0 || vSoft.IndexOf("linux") > 0 || vSoft.IndexOf("SunOS") > 0 || vSoft.IndexOf("BSD") > 0 || vSoft.IndexOf("Mac") > 0)
            vOS = "Unix & Unix 类";

        string vYear = System.DateTime.Now.Year.ToString();//年
        string vMonth = System.DateTime.Now.Month.ToString();//月
        string vDay = System.DateTime.Now.Day.ToString();//日
        string vHour = System.DateTime.Now.Hour.ToString();	//时
        string vTime = System.DateTime.Now.ToString();//时间 xxxx-xx-xx xx:xx:xx
        string vDate = System.DateTime.Now.Date.ToString();
        string vWeekDay = System.DateTime.Now.DayOfWeek.ToString();//星期
        string Tp_Ip = vIp;

        try
        {
            Tp_Ip = ii.Fn_IP(Tp_Ip);
            //查看Ip所在地区

            string vWhereB = ii.GetWhere(Tp_Ip);

            string sql1 = "update flux set refurbish=refurbish + 1";
            ii.ExecBool(sql1);//刷新

            if (Session["online"] == null)
            {
                Session["online"] = "";
            }
            if (Session["online"].ToString() != "true")
            {
                string sql2 = "";
                DataTable dt2 = new DataTable();

                sql2 = "select cWhere,cCWC from cW where cWhere='" + vWhereB + "'"; //访问者来源操作
                dt2 = ii.Find(sql2);
                if (dt2.Rows.Count > 0)
                {
                    string sql3 = "update cW set cCWC = cCWC + 1 where cWhere='" + vWhereB + "'";
                    ii.ExecBool(sql3);
                }
                else
                {
                    if (vWhereB.Trim() != "")
                    {
                        string sql3 = "insert into cW (cWhere,cCWC) values ('" + vWhereB + "',1)";
                        ii.ExecBool(sql3);
                    }
                }
                sql2 = "select cReferer,cCRC from cR where cReferer='" + vReferer + "'";//访问者来源网址
                dt2 = ii.Find(sql2);
                if (dt2.Rows.Count > 0)
                {
                    string sql3 = "update cR set cCRC = cCRC + 1 where cReferer='" + vReferer + "'";
                    ii.ExecBool(sql3);
                }
                else
                {
                    if (vReferer.Trim() != "")
                    {
                        string sql3 = "insert into cR (cReferer,cCRC) values ('" + vReferer + "',1)";
                        ii.ExecBool(sql3);
                    }
                }
                sql2 = "update cH set cTHC=0 where DATEDIFF('h',HMD,Now()) > 1 and cHour=" + vHour;
                ii.ExecBool(sql2);
                sql2 = "update cD set cTDC=0 where DATEDIFF('d',DMD,Now()) > 1 and cDay=" + vDay;
                ii.ExecBool(sql2);
                sql2 = "update cM set cTMC=0 where DATEDIFF('m',MMD,Now()) > 1 and cMonth=" + vMonth;
                ii.ExecBool(sql2);
                sql2 = "update (select * from (select top 1 * from last20vister order by cCT asc,id asc) as lv,(select * from cM where cMonth=" + vMonth + " ) as lcM,(select * from cD where cDay=" + vDay + " ) as lcD,(select * from cH where cHour=" + vHour + " ) as lcH,(select * from cE where cExplorer='" + vExplorer + "') as lcE,(select * from cO where cOS='" + vOS + "') as lcO ) as c set cCT=now(),cCIP='" + vIp + "',cCOS='" + vOS + "',cCEXP='" + vExplorer + "',cCW='" + vWhereB + "',cCR='" + iReferer + "',cTMC=cTMC+1,cCMC=cCMC+1,MMD=now(),cTDC=cTDC+1,cCDC=cCDC+1,DMD=now(),cTHC=cTHC+1,cCHC=cCHC+1,HMD=now(),cCEC=cCEC+1,cCOC=cCOC+1";
                ii.ExecBool(sql2);

                Session["online"] = "true";
            }
        }
        catch
        {
            Response.Write("document.write(\"ERROR ON SERVER\");");
        }

        //0或者没有不显示,1显示图标,2显示文字
        string style = "0";
        if (Request["style"] != null)
            style = Request["style"].ToString();
        if (style == "1")
            Style1();
        else if (style == "2")
            Style2();
        else if (style == "3")
            Style3();
    }

    private void Style1()
    {
        try
        {
            int picnum = 8;
            string nnum = "8";// System.Configuration.ConfigurationSettings.AppSettings["picnum"];
            string nums = "4,5,6,7,8,9,10,11,12";
            if (nums.IndexOf(nnum) > 0)
                picnum = Int32.Parse(nnum);

            string counternum = "0";
            string sql = "select sum(cCMC) as ccount from cM";
            DataTable dt = new DataTable();
            dt = ii.Find(sql);
            if (dt.Rows[0][0].ToString() != "")
                counternum = dt.Rows[0][0].ToString();

            int ct = picnum - counternum.Length;
            for (int i = 0; i < ct; i++)
            {
                counternum = "0" + counternum;
            }
            string pic = "";
            string dispic = "";
            for (int i = 0; i < counternum.Length; i++)
            {
                pic = "<img src=slt/" + counternum.Substring(i, 1) + ".gif alt='" + counternum.Substring(i, 1) + "'>";
                dispic = "document.write(\"" + pic + "\");";
                Response.Write(dispic);
            }
            dt.Dispose();
        }
        catch
        {
            Response.Write("document.write(\"ERROR ON SERVER\");");
            return;
        }
    }

    private void Style2()
    {
        try
        {
            string disdoc = "<TABLE style='font-size:12px;' cellSpacing='0' cellPadding='0' width='160' border='0'><TR align='center'><TD colspan='2'>";
            string counternum = "0";
            string sql = "select sum(cCMC) as ccount from cM";
            DataTable dt = new DataTable();
            dt = ii.Find(sql);
            if (dt.Rows[0][0].ToString() != "")
                counternum = dt.Rows[0][0].ToString();

            disdoc = disdoc + "总访问量：<font color='#FF0000'>" + counternum + "</font></TD></TR><TR><TD>";

            //counternum = "0";
            //sql = "select sum(cTDC) from cD where cDay = " + System.DateTime.Now.Day.ToString();
            //dt = ii.Find(sql);
            //if (dt.Rows[0][0].ToString() != "")
            //    counternum = dt.Rows[0][0].ToString();

            //disdoc = disdoc + "&nbsp;今日：" + counternum + "</TD><TD>";

            counternum = "0";
            sql = "select sum(cTMC) from cM where cMonth = " + System.DateTime.Now.Month.ToString();
            dt = ii.Find(sql);
            if (dt.Rows[0][0].ToString() != "")
                counternum = dt.Rows[0][0].ToString();

            disdoc = disdoc + "&nbsp;本月：" + counternum + "</TD><TD>";//</TR><TR>

            //counternum = "0";
            //sql = "select sum(cTDC) from cD where cDay = " + System.DateTime.Now.AddDays(-1).Day.ToString();
            //dt = ii.Find(sql);
            //if (dt.Rows[0][0].ToString() != "")
            //    counternum = dt.Rows[0][0].ToString();

            //disdoc = disdoc + "&nbsp;昨日：" + counternum + "</TD><TD>";

            counternum = "0";
            sql = "select sum(cTMC) from cM where cMonth = " + System.DateTime.Now.AddMonths(-1).Month.ToString();
            dt = ii.Find(sql);
            if (dt.Rows[0][0].ToString() != "")
                counternum = dt.Rows[0][0].ToString();
            dt.Dispose();
            disdoc = disdoc + "&nbsp;上月：" + counternum + "</TD></TR></TABLE>";

            disdoc = "document.write(\"" + disdoc + "\");";
            Response.Write(disdoc);
        }
        catch
        {
            Response.Write("document.write(\"ERROR ON SERVER\");");
            return;
        }
    }

    private void Style3()
    {
        try{
            string counternum = "0";
            string disdoc = "";
            string sql = "select sum(cCMC) as ccount from cM";
            DataTable dt = new DataTable();
            dt = ii.Find(sql);
            if (dt.Rows[0][0].ToString() != "")
                counternum = dt.Rows[0][0].ToString();

            disdoc =  "<font color='#333333'>" + counternum + "</font>";
            dt.Dispose();
            disdoc = "document.write(\"" + disdoc + "\");";
            Response.Write(disdoc);
        }
        catch
        {
            Response.Write("document.write(\"ERROR ON SERVER\");");
            return;
        }
    }
}